package com.shine.app.ui;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.bean.BookingConfirmationBean;
import com.shine.app.bean.GolfCourseDetailBean;
import com.shine.app.bean.responses.ValidateResponse;
import com.shine.app.commons.Calendars;
import com.shine.app.commons.UIHelper;
import com.shine.app.inject.InjectView;
import com.shine.app.inject.Injector;

public class SlideCancelTeeTimeActivity extends BaseActivity implements
		OnClickListener {
	@InjectView(R.id.head_back)
	private ImageView head_backImageView;
	@InjectView(R.id.head_name)
	private TextView head_nameTextView;

	@InjectView(R.id.keep)
	private Button keepButton;
	@InjectView(R.id.cancel)
	private Button cancelButton;
	@InjectView(R.id.progressBar)
	private ProgressBar progressBar;
	@InjectView(R.id.reservation)
	private TextView reservationTextView;
	@InjectView(R.id.name)
	private TextView nameTextView;
	@InjectView(R.id.date)
	private TextView dateTextView;
	@InjectView(R.id.course)
	private TextView courseTextView;
	private BookingConfirmationBean bookingConfirmationBean;

	private CancelBookingAsyncTask cancelBookingAsyncTask;
	private CourseDetailAsyncTask courseDetailAsyncTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.slide_cancelbooking);
		Injector.get(this).inject();

		init();
		head_backImageView.setOnClickListener(this);
		keepButton.setOnClickListener(this);
		cancelButton.setOnClickListener(this);
	}

	private void init() {
		progressBar.setVisibility(View.GONE);
		bookingConfirmationBean = (BookingConfirmationBean) getIntent()
				.getSerializableExtra("bookingConfirmationBean");
		if (bookingConfirmationBean != null) {
			reservationTextView.setText("#"
					+ bookingConfirmationBean.getReservationId());
			nameTextView.setText(bookingConfirmationBean.getFullName());
			dateTextView.setText(Calendars.getInstance().formatTeeTimeDate(
					bookingConfirmationBean.getRequestedTeeTime()));
			courseDetailAsyncTask = new CourseDetailAsyncTask();
			courseDetailAsyncTask
					.execute(bookingConfirmationBean.getCourseId());
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.head_back:
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
			break;

		case R.id.keep:
			keepButton.startAnimation(buttonAnimations);
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
			break;

		case R.id.cancel:
			cancelButton.startAnimation(buttonAnimations);
			if (bookingConfirmationBean != null) {
				String reservationId = bookingConfirmationBean
						.getReservationId();
				String golfId = bookingConfirmationBean.getGolferId();
				cancelBookingAsyncTask = new CancelBookingAsyncTask();
				cancelBookingAsyncTask.execute(reservationId, golfId);
			}
			break;

		default:
			break;
		}
	}

	public class CourseDetailAsyncTask extends
			AsyncTask<Object, Integer, GolfCourseDetailBean> {
		private int courseId;

		@Override
		protected GolfCourseDetailBean doInBackground(Object... params) {
			GolfCourseDetailBean bean = null;
			courseId = (Integer) params[0];
			bean = appContext.courseDetail(courseId);
			return bean;
		}

		@Override
		protected void onPostExecute(GolfCourseDetailBean result) {
			super.onPostExecute(result);
			if (result != null) {
				courseTextView.setText(result.getName());
			}
			progressBar.setVisibility(View.GONE);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
		}

	}

	public class CancelBookingAsyncTask extends
			AsyncTask<Object, Integer, ValidateResponse> {
		private String reservationid;
		private String golferId;

		@Override
		protected ValidateResponse doInBackground(Object... params) {
			ValidateResponse response = null;
			reservationid = (String) params[0];
			golferId = (String) params[1];
			response = appContext.cancelReservation(reservationid, golferId);
			return response;
		}

		@Override
		protected void onPostExecute(ValidateResponse result) {
			super.onPostExecute(result);
			if (result != null) {
				if (result.isData()) {
					Intent intent = new Intent(SlideCancelTeeTimeActivity.this,
							SlideCancelledTeeTimeActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					SlideCancelTeeTimeActivity.this.startActivity(intent);
					AppManager.getAppManager().finishActivity();
					overridePendingTransition(R.anim.slide_in_up, R.anim.anim);
				} else {
					UIHelper.toastDialogs(SlideCancelTeeTimeActivity.this,
							result.getErrorMessage(), R.string.msg_yes);
				}
			} else {
				UIHelper.toastDialog(SlideCancelTeeTimeActivity.this,
						R.string.msg_reservation_cancel_failed,
						R.string.msg_yes);
			}
			progressBar.setVisibility(View.GONE);
			keepButton.setEnabled(true);
			cancelButton.setEnabled(true);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
			keepButton.setEnabled(false);
			cancelButton.setEnabled(false);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (cancelBookingAsyncTask != null
				&& cancelBookingAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			cancelBookingAsyncTask.cancel(true);
		}
		if (courseDetailAsyncTask != null
				&& courseDetailAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			courseDetailAsyncTask.cancel(true);
		}
	}

}
